package com.cy.service;

import com.cy.spring.BeanPostProcessor;
import com.cy.spring.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Component
public class WdpBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object beanPostProcessorBeforeInit(String beanName,Object bean) {
        if(beanName.equals("userService")){
            System.out.println("11111");
        }
        return bean;
    }

    @Override
    public Object beanPostProcessorAfterInit(String beanName,Object bean) {
        if(beanName.equals("userService")){
            Object instance = Proxy.newProxyInstance(WdpBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("切面逻辑");
                    return method.invoke(bean,args);
                }
            });
            return instance;
        }
        return bean;
    }
}
